<?php

namespace app\api\model;

use app\admin\model\coupon\Coupon;
use app\admin\model\Shop;
use app\admin\model\User;
use app\admin\model\user\Coupon as UserCoupon;
use think\Db;
use think\Model;


class OrderCreate extends Model
{


    public function ordercreate($data)
    {
        $order_model = new Order();

        $order_sn = date("Ymdhis") . sprintf("%08d", $data['user_id']) . mt_rand(1000, 9999);

        $calculate = $this->calculate($data);

        $order_data = [
            'user_id'=>$data['user_id'],
            'order_no'=> $order_sn,
            'shop_id'=>$data['shop_id'],
            'shop_name'=>$calculate['shop_info']['shop_name'],
            'order_money'=>$calculate['order_money'],
            'pay_money'=>$calculate['pay_money'],
            'coupon_money'=>$calculate['coupon_money'],
            'coupon_id'=>$data['user_coupon_id'],
            'create_type'=>$data['create_type'],
            'cashback_money'=>$calculate['cashback_money'],
            'order_status_name'=>'待支付'
        ];

        //$wechatUser = User::get($data['user_id']);

        $res = $order_model->save($order_data);

        if ($res){
            //$request = request();
            //$notifyurl =  $request->root(true)."/api/order/epay";

            //$pay = \addons\epay\library\Service::submitOrder($order_data['pay_money'],$order_sn,'wechat',$order_data['shop_name'],$notifyurl,'','miniapp',$wechatUser['wechat_open_id']);
            return $order_data;
        } else {
            return false;
        }
    }

    public function calculate($data)
    {
        try {
            $shop_info = Shop::get($data['shop_id']);

            $coupon_list = UserCoupon::myGoodsCoupon($data['user_id'], $data['shop_id'], $data['money']);

            $userCoupon = null;
            $order_money = $data['money'];
            $pay_money = $data['money'];
            $coupon_money = 0;

            //校验领取和是否可使用
            if ($data['user_coupon_id']) {
                $userCouponModel = new UserCoupon();
                $userCoupon = $userCouponModel->checkUserOrUse($data['user_coupon_id'], $data['user_id']);
            }

            if (!empty($userCoupon)) {
                //校验优惠券
                $couponModel = new Coupon();
                $coupon = $couponModel->getCoupon($userCoupon['coupon_id'])
                    ->checkCoupon()
                    ->checkOpen()
                    ->checkUseTime($userCoupon['createtime']);


                $coupon_money = $coupon->doBuy($order_money);
                $coupon_money = $coupon_money[1];

                //判断优惠金额是否超出总价，超出则直接设定优惠金额为总价
                //$orderInfo['discount'] = $coupon_money > $order_money ? $order_money : $coupon_money;
            }

            if ($coupon_money>0){
                $pay_money = bcsub($order_money,$coupon_money,2);
            }
            $cashback_money = 0;
            if ($shop_info['cashback_ratio']){
                $cashback_money = $pay_money*$shop_info['cashback_ratio']/100;
            }
            $orderInfo = [
                'shop_info'=>$shop_info->toArray(),
                'coupon'          => $coupon_list,
                'order_money'=>$order_money,
                'pay_money'=>$pay_money,
                'coupon_money'=>$coupon_money,
                'cashback_money'=>$cashback_money,
            ];

            return $orderInfo;
        }  catch (\Exception $e) {

            throw new \Exception($e->getMessage());
        }

    }

}
